﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using CS.CleverNotes.Entities;
using CS.CleverNotes.Infrastructure;
using Microsoft.AspNet.Authorization;

namespace CS.CleverNotes.Controllers
{
    [Produces("application/json")]
    [Route("api/[controller]")]
 //   [Authorize]
    public class TodoController : Controller
    {
        private CleverNotesContext _context;

        public TodoController(CleverNotesContext context)
        {
            _context = context;
        }

        // GET: api/Todo
        [HttpGet]
        public IEnumerable<Todo> Todo(bool completed = false, int? parentId = null)
        {
            var result =  _context.ToDo.Where(x => x.ParentId == parentId 
                                                && x.Completed == completed)
                                       .OrderByDescending(x => x.DateCreated);
            return result;
        }

        // POST: api/Todo
        [HttpPost]
        public async Task<IActionResult> Todo([FromBody] Todo todo)
        {
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }

            _context.ToDo.Add(todo);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (TodoExists(todo.Id))
                {
                    return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
                }
                else
                {
                    throw;
                }
            }

            return Ok(todo);
        }

        // PUT: api/Todo/5
        [HttpPut("{id}")]
        public async Task<IActionResult> Todo([FromRoute] int id, [FromBody] Todo todo)
        {
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }

            if (id != todo.Id)
            {
                return HttpBadRequest();
            }

            _context.Entry(todo).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TodoExists(id))
                {
                    return HttpNotFound();
                }
                else
                {
                    throw;
                }
            }

            return  Ok(todo);
        }

        // DELETE: api/Todo/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Todo([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return HttpBadRequest(ModelState);
            }

            Todo todo = await _context.ToDo.SingleAsync(m => m.Id == id);
            if (todo == null)
            {
                return HttpNotFound();
            }

            _context.ToDo.Remove(todo);
            await _context.SaveChangesAsync();

            return Ok(todo);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _context.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool TodoExists(int id)
        {
            return _context.ToDo.Count(e => e.Id == id) > 0;
        }
    }
}